# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# -*- coding:utf-8 -*-

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter

import json,csv
class MyScrapyPipeline:
    def __init__(self):
        # 打开文件，指定方式为写，利用第3个参数把csv写数据时产生的空行消除
        # self.f = open("myproject.csv", "a", newline="")
        # # 设置文件第一行的字段名，注意要跟spider传过来的字典key名称相同
        # self.fieldnames = ["name"]
        # # 指定文件的写入方式为csv字典写入，参数1为指定具体文件，参数2为指定字段名
        # self.writer = csv.DictWriter(self.f, fieldnames=self.fieldnames)
        # # 写入第一行字段名，因为只要写入一次，所以文件放在__init__里面
        # self.writer.writeheader()
        pass

    # 执行一次
    def open_spider(self,spider):
        self.file  = open('a.csv','w')

    # 一直执行
    def process_item(self, item, spider):
        content = json.dumps(dict(item),ensure_ascii=False)+"\n"
        print("TITLE:",item['name'])
        print('URL',item['url'])
        print('CONTENT',item['content'])
        self.file.write(content)

        # 写入spider传过来的具体数值
        # self.writer.writerow(content.encode('utf-8'))

        return item

    def close_spider(self,spider):
        self.file.close()



